package com.comp490.navigation;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;

import com.comp490.shooter.MainActivity;
import com.comp490.shooter.R;
import com.comp490.sound.SoundManager;

public class MainMenu extends FragmentActivity
{
    private final String SELECTION_FRAG = "selection fragment";
    private final String TITLE_FRAG = "title fragment";
    private final String OPTIONS_FRAG = "options fragment";
    private final String HIGHSCORES_FRAG = "highscores fragment";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_menu);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        if(savedInstanceState == null)
        {
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();

            // add Selection fragment
            Fragment newFrag = new MainMenuSelectionFrag();
            transaction.add(R.id.mainmenu_selection_container, newFrag,
                    SELECTION_FRAG);

            // add MainView fragment
            newFrag = new MainMenuTitleScreenFrag();
            transaction.add(R.id.mainmenu_mainview_container, newFrag,
                    TITLE_FRAG);

            transaction.commit();
        }

        setSoundOptions();
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        SoundManager.getInstance(MainMenu.this);
        SoundManager.playBackgroundMusic(SoundManager.MAIN_MENU_BG);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        SoundManager.getInstance(MainMenu.this);
        SoundManager.playBackgroundMusic(SoundManager.MAIN_MENU_BG);
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        SoundManager.getInstance(MainMenu.this);
        SoundManager.pauseBackgroundMusic();
    }

    @Override
    public void onBackPressed()
    {
        SoundManager.playSoundEfx(SoundManager.BUTTON_CLICK);
        showExitDialog();
    }

    /** Handles all button click events for buttons on the Main Menu fragments */
    public void onButtonClick(View v)
    {
        FragmentTransaction trans = getSupportFragmentManager()
                .beginTransaction();
        trans.setCustomAnimations(R.anim.slide_out_right, R.anim.slide_in_left);
        SoundManager.playSoundEfx(SoundManager.BUTTON_CLICK);

        switch(v.getId())
        {
            case R.id.button_mainmenu_home:
                if(getSupportFragmentManager().findFragmentByTag(TITLE_FRAG) == null)
                {
                    Fragment fragment = new MainMenuTitleScreenFrag();
                    trans.replace(R.id.mainmenu_mainview_container, fragment,
                            TITLE_FRAG);
                    SoundManager.playSoundEfx(SoundManager.SWAP_MENU);
                    trans.commit();
                }
                break;

            case R.id.button_mainmenu_options:
                if(getSupportFragmentManager().findFragmentByTag(OPTIONS_FRAG) == null)
                {
                    Fragment fragment = new MainMenuOptionsFrag();
                    trans.replace(R.id.mainmenu_mainview_container, fragment,
                            OPTIONS_FRAG);
                    SoundManager.playSoundEfx(SoundManager.SWAP_MENU);
                    trans.commit();
                }
                break;

            case R.id.button_mainmenu_highscore:
                if(getSupportFragmentManager().findFragmentByTag(
                        HIGHSCORES_FRAG) == null)
                {
                    Fragment fragment = new MainMenuHighScoresFrag();
                    trans.replace(R.id.mainmenu_mainview_container, fragment,
                            HIGHSCORES_FRAG);
                    SoundManager.playSoundEfx(SoundManager.SWAP_MENU);
                    trans.commit();
                }
                break;

            case R.id.button_mainmenu_exit:
                showExitDialog();
                break;

            case R.id.button_mainmenu_title_startgame:
                startActivity(new Intent(MainMenu.this, MainActivity.class));
                finish();
                break;
        }
    }

    private void setSoundOptions()
    {
        SharedPreferences soundoptions = getSharedPreferences(
                SoundManager.SOUND_PREFS, 0);

        if(soundoptions.getBoolean(SoundManager.MUSIC_ON, true))
            SoundManager.unMuteMusic();
        else
            SoundManager.muteMusic();

        if(soundoptions.getBoolean(SoundManager.FX_ON, true))
            SoundManager.unMuteSoundFx();
        else
            SoundManager.muteSoundFx();
    }

    private void showExitDialog()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainMenu.this);
        builder.setIcon(R.drawable.spaceship_tiny);
        builder.setTitle("Exit Lead Hammer?");        
        builder.setCancelable(true);        

        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                SoundManager.playSoundEfx(SoundManager.BUTTON_CLICK);
                dialog.dismiss();
                finish();
            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                SoundManager.playSoundEfx(SoundManager.BUTTON_CLICK);
                dialog.dismiss();
            }
        });

        builder.create().show();
    }

}